﻿using AutoMapper;
using MediatR;
using UserLicense.Applicationes.Command;
using UserModels;
using UserRepositorys.Intface;

namespace UserLicense.Applicationes.CommandHandler
{
    public class UserAddCommandHandler:IRequestHandler<UserAddCommand,int>
    {
        private readonly IUserRepository userRepository;
        private readonly  IMapper _mapper;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="userRepository"></param>
        public UserAddCommandHandler(IUserRepository userRepository,IMapper mapper)
        {
            this.userRepository = userRepository;
            _mapper = mapper;
        }

        /// <summary>
        /// 用户添加
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> Handle(UserAddCommand request, CancellationToken cancellationToken)
        {
            UserInfoModel model =_mapper.Map<UserAddCommand,UserInfoModel>(request);
           return await userRepository.AddUser(model);
        }
    }
}
